package eventplanner.rest;

import eventplanner.ejb.ServiceFacade;
import eventplanner.ejb.dao.TokenDao;
import eventplanner.ical.ICalConverter;

import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/token")
public class TokenService {

	@Inject
	private ServiceFacade serviceFacade;

	@Inject
	private TokenDao dao;

	@GET
	@Path("/tokens")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getUserTokens() {
		return Response.ok(serviceFacade.getUserTokens()).build();
	}

	@GET
	@Path("/{tokenString}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getToken(@PathParam("tokenString") String tokenString) {
		return Response.ok(serviceFacade.getToken(tokenString)).build();
	}

	@POST
	@Path("/new")
	@Produces(MediaType.APPLICATION_JSON)
	public Response createNewToken() {
		return Response.status(Response.Status.CREATED).entity(serviceFacade.createNewToken()).build();
	}

	@POST
	@Path("/{tokenId}/feed/add/{feedId}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response addFeedToToken(@PathParam("tokenId") long tokenId, @PathParam("feedId") long feedId) {
		return Response.status(Response.Status.CREATED).entity(serviceFacade.addFeedToToken(tokenId, feedId) ? "True" : "False").build();
	}

	@POST
	@Path("/{tokenId}/feed/del/{feedId}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response delFeedFromToken(@PathParam("tokenId") long tokenId, @PathParam("feedId") long feedId) {
		return Response.status(Response.Status.CREATED).entity(serviceFacade.delFeedFromToken(tokenId, feedId) ? "True" : "False").build();
	}

	@POST
	@Path("/{tokenId}/tag/add/{tag}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response addTagToToken(@PathParam("tokenId") long tokenId, @PathParam("tag") String tag) {
		return Response.status(Response.Status.CREATED).entity(serviceFacade.addTagToToken(tokenId, tag) ? "True" : "False").build();
	}

	@POST
	@Path("/{tokenId}/tag/del/{tag}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response delTagFromToken(@PathParam("tokenId") long tokenId, @PathParam("tag") String tag) {
		return Response.status(Response.Status.CREATED).entity(serviceFacade.delTagFromToken(tokenId, tag) ? "True" : "False").build();
	}

	@GET
	@Path("/{tokenId}/tags")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getTags(@PathParam("tokenId") long id) {
		return Response.ok(serviceFacade.getTagsFromToken(id)).build();
	}

	@GET
	@Path("/{tokenId}/targets")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getTargets(@PathParam("tokenId") long id) {
		return Response.ok(serviceFacade.getTargetFromToken(id)).build();
	}

	@GET
	@Path("/{tokenId}/organizers")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getOrganizers(@PathParam("tokenId") long id) {
		return Response.ok(serviceFacade.getOrganizerFromToken(id)).build();
	}

	@GET
	@Path("/ical/{token}")
	@Produces("text/calendar")
	public Response getIcal(@PathParam("token") String token) {
		return Response.ok(new ICalConverter().convert(dao.getEventsForToken(token))).build();
	}

	@POST
	@Path("/{tokenId}/target/add/{target}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response addTargetToToken(@PathParam("tokenId") long tokenId, @PathParam("target") String target) {
		return Response.ok(serviceFacade.addTargetToToken(tokenId, target) ? "True" : "False").build();
	}

	@POST
	@Path("/{tokenId}/target/del/{target}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response delTargetToToken(@PathParam("tokenId") long tokenId, @PathParam("target") String target) {
		return Response.ok(serviceFacade.delTargetToToken(tokenId, target) ? "True" : "False").build();
	}

	@POST
	@Path("/{tokenId}/organizer/add/{target}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response addOrganizerToToken(@PathParam("tokenId") long tokenId, @PathParam("target") String target) {
		return Response.ok(serviceFacade.addOrganizerToToken(tokenId, target) ? "True" : "False").build();
	}

	@POST
	@Path("/{tokenId}/organizer/del/{target}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response delOrganizerToToken(@PathParam("tokenId") long tokenId, @PathParam("target") String target) {
		return Response.ok(serviceFacade.delOrganizerToToken(tokenId, target) ? "True" : "False").build();
	}

	@GET
	@Path("/{tokenId}/feeds")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getFeeds(@PathParam("tokenId") long id) {
		return Response.ok(serviceFacade.getFeeds(id)).build();
	}


}
